//
//  LoginViewController.m
//  通讯录-02
//
//  Created by 伍银荣 on 2021/6/29.
//  Copyright © 2021 com.example. All rights reserved.
//

#import "LoginViewController.h"
#import "ContactViewController.h"

@interface LoginViewController ()
@property (strong, nonatomic) IBOutlet UITextField *userNameTF;
@property (strong, nonatomic) IBOutlet UITextField *passwordTF;
@property (strong, nonatomic) IBOutlet UIButton *loginBtn;
@property (strong, nonatomic) IBOutlet UISwitch *remindPwdSwich;
@property (strong, nonatomic) IBOutlet UISwitch *autoLoginSwich;

@end

@implementation LoginViewController
- (IBAction)loginBtnClicked:(UIButton *)sender {
    if ([self.userNameTF.text isEqualToString:@"1"] && [self.passwordTF.text isEqualToString:@"1"]) {
        NSLog(@"登陆成功");
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            //执行跳转的方法(单纯脱了线,但是没有执行这个方法是不能跳转的)
            [self performSegueWithIdentifier:@"loginToContact" sender:nil];
            
            //登录成功后,保存开关状态和账号密码
            NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
//            [userDefaults setBool:self.remindPwdSwich.isOn forKey:@"remindOn"];
//            [userDefaults setBool:self.autoLoginSwich.isOn forKey:@"autoLoginOn"];
            
            [userDefaults setObject:self.remindPwdSwich.isOn ? @"Y" : @"N" forKey:@"remindOn"];
            [userDefaults setObject:self.autoLoginSwich.isOn ? @"Y" : @"N" forKey:@"autoLoginOn"];
            [userDefaults setObject:self.userNameTF.text forKey:@"userName"];
            [userDefaults setObject:self.passwordTF.text forKey:@"password"];
            
            NSLog(@"remind - %@---autoLogin - %@",[userDefaults valueForKey:@"remindOn"], [userDefaults valueForKey:@"autoLoginOn"]);
            
            [userDefaults synchronize];  //立即写入
        });
    }
}

//每次跳转都会执行这个方法
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    ContactViewController *contactVC = segue.destinationViewController;
    contactVC.title = [NSString stringWithFormat:@"%@的联系人列表",self.userNameTF.text];
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.remindPwdSwich.on = NO;
    self.autoLoginSwich.on = NO;
    
    NSLog(@"home = %@",NSHomeDirectory());
    [self.userNameTF addTarget:self action:@selector(textChanged) forControlEvents:UIControlEventEditingChanged];
    [self.passwordTF addTarget:self action:@selector(textChanged) forControlEvents:UIControlEventEditingChanged];
    
   
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    self.userNameTF.text = [userDefaults valueForKey:@"userName"];
     //恢复开关状态
    BOOL remindPwdSwichState = [[userDefaults valueForKey:@"remindOn"] isEqualToString:@"Y"];
    BOOL autoLoginSwichState = [[userDefaults valueForKey:@"autoLoginOn"] isEqualToString:@"Y"];
    self.remindPwdSwich.on = remindPwdSwichState;
    self.autoLoginSwich.on = autoLoginSwichState;
    NSLog(@"---remind = %@,autologin = %@",[userDefaults valueForKey:@"remindOn"],[userDefaults valueForKey:@"autoLoginOn"]);
    NSLog(@"remind = %d, autologin = %d",remindPwdSwichState,autoLoginSwichState);
    
    if (self.remindPwdSwich.isOn) {
        self.passwordTF.text = [userDefaults valueForKey:@"password"];
    }
    if (self.autoLoginSwich.isOn) {
        [self loginBtnClicked:self.loginBtn];
    }
    [self textChanged];
}

- (void)textChanged {
    if (self.userNameTF.text.length > 0 && self.passwordTF.text.length > 0) {
        self.loginBtn.enabled = YES;
    }
}


//记住密码开关的点击事件
- (IBAction)remindPwdSwichClicked:(UISwitch *)sender {
    //如果记住密码开关关闭,那么也要关闭自动登录
    if (!sender.isOn) {
        [self.autoLoginSwich setOn:NO animated:YES];
    }
}

//自动登录开关的点击事件
- (IBAction)autoLoginSwichClicked:(UISwitch *)sender {
    //如果开启自动登录开关的话,那么记住密码也要开启
    if (sender.isOn) {
        [self.remindPwdSwich setOn:YES animated:YES];
    }
}







/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
